#!/usr/bin/env node

// @ts-nocheck

process.on('unhandledRejection', (err) => {
  console.log('SIGINT');
  process.exit();
});

const { workerRun } = require('./lib/workerRun');
const { userSelect } = require('./lib/uiUtil');
const formatter = require('./lib/eslint-format');

const { _: args, ...cmdOpts } = require('minimist')(process.argv.slice(2));

const config = {
  'voucher-common': {
    message: ' - voucher-common 耦合检测',
    globPattern: 'src/voucher-common/**/*.{ts,tsx,js}'
  },
  components: {
    message: ' - components 耦合检测',
    globPattern: 'src/components/**/*.{ts,tsx,js}'
  },
  apphub: {
    message: ' - apphub 耦合检测',
    globPattern: 'src/apphub/**/*.{ts,tsx,js}'
  },
  'apphub-components-voucher-common': {
    message: ' - apphub,components,voucher-common 耦合检测',
    globPattern: 'src/{apphub,components,voucher-common}/**/*.{ts,tsx,js}'
  },
  hkj: {
    message: ' - hkj 耦合检测',
    globPattern: 'src/hkj/**/*.{ts,tsx,js}'
  },
  unused: {
    message: ' - fix unused import',
    globPattern: 'src/**/*.{ts,tsx,js}'
  },
  importType: {
    message: ' - fix import type ',
    globPattern: 'src/**/*.{ts,tsx}'
  },
  'ban-this-in-arrowfunction': {
    message: ' - 无效this检测 ',
    globPattern: 'src/**/*.{ts,tsx}'
  }
}

const TypeMap = {
  'apphub-components-voucher-common': 'apphub'
}

main();

// 替换模块引用路径
async function main () {
  let type = args[0];
  if (!config[type]) {
    const selectItems = Object.entries(config).map(([k,v]) => ({message: v.message, value: k}))
    type = await userSelect('请选择合适的操作', selectItems);
  }
  const globPattern = config[type].globPattern

  // remap
  type = TypeMap[type] || type

  // 检测参数
  await workerRun(require.resolve('./mod/eslint.js'), {
    type,
    globPattern,
    onResult: (result) => {
      const results = [].concat(...result);
      console.log(formatter(results));
      return 'ok';
    },
  });
};

